﻿using Microsoft.AspNetCore.Mvc;
using WebAPI.Entities;
using WebAPI.Services;

namespace WebAPI.Controllers;

[ApiController]
[Route("api/[controller]/[Action]")]
public class CommentController : ControllerBase
{
    private CommentService _commentService = new CommentService();
    
    [HttpPost(Name = "PublishComment")]
    public ActionResult PublishComment(Comment comment)
    {
        bool flag = _commentService.AddComment(comment);
        if (!flag)
        {
            return BadRequest("发布评论失败，请稍后重试");
        }
        return Ok();
    }
    
    [HttpGet(Name = "GetComments")]
    public ActionResult<List<Comment>> GetComments(string article_id)
    {
        List<Comment> comments = _commentService.GetAllComments(article_id);
        if (comments.Count == 0)
        {
            return BadRequest("获取评论失败，请稍后重试");
        }
        return Ok(comments);
    }
    
    [HttpPut(Name = "DeleteComment")]
    public ActionResult<Article> DeleteComment(Comment comment)
    {
        bool flag = _commentService.DeleteComment(comment);
        if (!flag)
        {
            return BadRequest("删除评论失败，请稍后重试");
        }
        return Ok();
    }
    
    [HttpPut(Name = "UpdateComment")]
    public ActionResult<Article> UpdateComment(Comment comment)
    {
        bool flag = _commentService.UpdateComment(comment);
        if (!flag)
        {
            return BadRequest("删除文章失败，请稍后重试");
        }
        return Ok();
    }
}